/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.button;

import org.apache.wicket.markup.html.WebMarkupContainer;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * @author Nicolas Giard
 */
public abstract class JQButton extends WebMarkupContainer implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQButtonPlugin plugin;

	/** JQuery default options */
	private JQButtonOptions options = new JQButtonOptions();

	private String functionBody = "";

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQButton(String id, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQButtonPlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQButtonPlugin(key);
		}
		this.plugin = (JQButtonPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 */
	public JQButton(String id)
	{
		this(id, null);
	}

	public JQButtonOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQButtonOptions.class))
		{
			this.options = (JQButtonOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQButton.class, options.getClass(), JQButtonOptions.class);
		}
	}

	public JQButtonPlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQButtonPlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return buildScript();
	}

	public String getAfterAjaxUpdateScript()
	{
		return buildScript();
	}

	private String buildScript()
	{
		this.onClick();
		StringBuffer sb = new StringBuffer();
		sb.append("$('#");
		sb.append(this.getMarkupId());
		sb.append("').click(function(){");
		sb.append(functionBody);
		sb.append("});");
		return sb.toString();
	}

	public void setFunctionBody(String body)
	{
		this.functionBody = body;
	}

	public abstract void onClick();
}